package com.activemq.dlq;

import cn.hutool.core.date.DateUtil;
import com.activemq.cluster.ClusterProd;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.UUID;

public class DLQSender {
    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ClusterProd.CLUSTER_URL);
        try (Connection connection = factory.createConnection()) {
            connection.start();
            try (Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE)) {
                Queue queue = session.createQueue("spring-active-queue");
                //超时会移入死信队列，但是如果设置processExpired="false"，则过期不会进入死信队列。
                try (MessageProducer producer = session.createProducer(queue)) {
                    producer.setTimeToLive(6000);
                    //正常情况下，非持久化的消息不会进入死信队列，但是配置了 processNonPersistent="true" 也会进入死信队列
//                    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                    TextMessage textMessage = session.createTextMessage("消息" + DateUtil.now() + UUID.randomUUID());
                    producer.send(textMessage);
                    session.commit();
                }
            }
        }
    }

}